package com.base.cn.platform.os.common.j2cache;

import org.slf4j.Logger;

import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * 初始化配置文件
 *
 * @author s.li
 * @date 2019/02/27
 */
public class J2CacheInitProperties {
    /**
     * 初始化配置文件
     * @param log
     */
    public static void init(Logger log){
        try {
            List<Map<String,String>> properKeyValueList = new ArrayList<>();
            for (J2CacheRegion region : J2CacheRegion.values()){
                Map<String,String> map = new HashMap<>();
                map.put(region.getKey(),region.getValue());
                properKeyValueList.add(map);
            }
            ClassLoader classLoader = J2CacheInitProperties.class.getClassLoader();
            URL url = classLoader.getResource("config/caffeine.properties");
            log.info("j2cache caffeine config file url:"+url.toString());
            Properties prop = new Properties();
            InputStream in = new BufferedInputStream(new FileInputStream(new File(url.getFile())));
            prop.load(in);     //加载属性列表
            in.close();
            FileOutputStream oFile = new FileOutputStream(new File(url.getFile()), false);//true表示追加打开
            properKeyValueList.forEach(map->{
                map.forEach((k,v)->{
                    prop.setProperty(k,v);
                });
            });
            prop.store(oFile,"The New properties file");
            oFile.close();
            log.info("j2cache properties file config success");
        } catch (Exception e) {
            log.error("j2cache properties file config error");
        }
    }
}
